package com.ruoyi.room.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.room.domain.Room;
import com.ruoyi.room.domain.RoomRecord;
import com.ruoyi.room.service.IRoomRecordService;
import com.ruoyi.room.service.IRoomService;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.service.IUserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 抽奖记录Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/room/record")
public class RoomRecordController extends BaseController {
    @Autowired
    private IRoomRecordService roomRecordService;
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IUserService userService;

    /**
     * 查询抽奖记录列表
     */
    @PreAuthorize("@ss.hasPermi('room:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(RoomRecord roomRecord) {
        startPage();
        List<RoomRecord> list = roomRecordService.selectRoomRecordList(roomRecord);
        for (RoomRecord record : list) {
            User user = userService.selectUserById(record.getUserId());
            Room room = roomService.selectRoomById(record.getRoomId());
            record.setPhone(user.getPhone());
            record.setNickName(user.getName());
            record.setRoomName(room.getName());
        }
        return getDataTable(list);
    }

    /**
     * 导出抽奖记录列表
     */
    @PreAuthorize("@ss.hasPermi('room:record:export')")
    @Log(title = "抽奖记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoomRecord roomRecord) {
        List<RoomRecord> list = roomRecordService.selectRoomRecordList(roomRecord);
        ExcelUtil<RoomRecord> util = new ExcelUtil<RoomRecord>(RoomRecord.class);
        util.exportExcel(response, list, "抽奖记录数据");
    }

    /**
     * 获取抽奖记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('room:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(roomRecordService.selectRoomRecordById(id));
    }

    /**
     * 新增抽奖记录
     */
    @PreAuthorize("@ss.hasPermi('room:record:add')")
    @Log(title = "抽奖记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RoomRecord roomRecord) {
        return toAjax(roomRecordService.insertRoomRecord(roomRecord));
    }

    /**
     * 修改抽奖记录
     */
    @PreAuthorize("@ss.hasPermi('room:record:edit')")
    @Log(title = "抽奖记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RoomRecord roomRecord) {
        return toAjax(roomRecordService.updateRoomRecord(roomRecord));
    }

    /**
     * 删除抽奖记录
     */
    @PreAuthorize("@ss.hasPermi('room:record:remove')")
    @Log(title = "抽奖记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(roomRecordService.deleteRoomRecordByIds(ids));
    }
}
